﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using AzureDiagMon.Common.Mvvm;
using AzureDiagMon.Common.Mvvm.Interfaces;
using GalaSoft.MvvmLight.Command;

namespace AzureDiagMon.Common.ViewModels
{
    [ViewModel("ModalDialog")]
    public sealed class ModalDialogViewModel : ViewModel
    {
        private IChildViewModel childViewModel;
        public IChildViewModel ChildViewModel
        {
            get { return childViewModel; }
            set
            {
                childViewModel = value;
                if (value != null)
                {
                    //ChildControl = childViewModel.UserControl as UserControl;
                    Title = childViewModel.Title;
                }
                else
                {
                    //ChildControl = null;
                    Title = null;
                }
                RaisePropertyChanged("ChildViewModel");
            }
        }

        private FrameworkElement childControl;
        public FrameworkElement ChildControl
        {
            get { return childControl; }
            set
            {
                childControl = value;
                if (childControl != null)
                {
                    ChildViewModel = childControl.DataContext as IChildViewModel;
                }
                else
                {
                    ChildViewModel = null;
                }
                RaisePropertyChanged("WindowClosing");
                RaisePropertyChanged("OkCommand");
                RaisePropertyChanged("CancelCommand");
                RaisePropertyChanged("ChildControl");
            }
        }

        private string title;
        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                RaisePropertyChanged("Title");
            }
        }

        public ICommand WindowClosing { get; private set; }
        public ICommand OkCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }

        /// <summary>
        /// Initializes a new instance of the ModalDialogViewModel class.
        /// </summary>
        public ModalDialogViewModel()
        {
            if (IsInDesignMode)
            {
            }
            else
            {
            }

            WindowClosing = new RelayCommand<CancelEventArgs>(HandleWindowClosing);

            OkCommand = new RelayCommand<Window>(
                (window) =>
                {
                    if (HandleOkCommand())
                    {
                        window.DialogResult = true;
                    }
                }, f => IsOkEnabled());

            CancelCommand = new RelayCommand<Window>(
                (window) =>
                {
                    if (HandleCancelCommand())
                    {
                        window.DialogResult = false;
                    }
                }, f => IsCancelEnabled());

        }

        public void HandleWindowClosing(CancelEventArgs args)
        {
            if (ChildViewModel != null)
                ChildViewModel.HandleWindowClosing(args);
        }

        public bool HandleOkCommand()
        {
            if (ChildViewModel != null)
                return ChildViewModel.HandleOkCommand();
            return false;
        }

        public bool IsOkEnabled()
        {
            if (ChildViewModel != null)
                return ChildViewModel.IsOkEnabled();
            return false;
        }

        public bool HandleCancelCommand()
        {
            if (ChildViewModel != null)
                return ChildViewModel.HandleCancelCommand();
            return false;
        }

        public bool IsCancelEnabled()
        {
            if (ChildViewModel != null)
                return ChildViewModel.IsCancelEnabled();
            return false;
        }

        //public override void Cleanup()
        //{
        //    // Clean own resources if needed

        //    base.Cleanup();
        //}
    }
}